#!/bin/bash

# 函数定义######################################################################

# 初始化全局数据################################################################
GPF_SUB_CMD_NAME=$(basename $0)
################################################################################

# Bash里使用getopts解析非选项参数: http://9.douban.com/subject/9127110/

declare -a NOA # Array used to store non-option argument
 
while [ $# -ne 0 ]
do
    # if OPTIND > $#+1, getopts will not change OPTIND's value,so set it to 0
    OPTIND=0
    while getopts : opt
    do
        case $opt in
            \?)
                echo "Invalid option -- $OPTARG"
                echo "Usage: $GPF_PROC_NAME $GPF_SUB_CMD_NAME"
                exit 0
                ;;
        esac
    done
 
    # if getopts find a non-option argument
    # it will stop parsing and return OPTIND
    # as index to the first non-option argument
    # 
    # if getopts doesn't find any non-option argument
    # it will set OPTIND=$#+1
    if [ $OPTIND -ne $(($#+1)) ]
    then
        shift $(($OPTIND-1))
        # This is just my favorate way to append element to an array in shell.
        # Feel free to change it
        NOA=(`echo ${NOA[*]}` $1)
        shift
    else
        break; # getopts doesn't find any non-option argument
    fi
done

################################################################################

declare -a ret

GPF_GIT_ROOT=$(git rev-parse --show-toplevel)
ret=$?
if [[ $ret != 0 ]]; then
    exit $ret
fi

GPF_SUB_CONFIG_FILE_PATH="$GPF_GIT_ROOT/$GPF_SUB_CONFIG_FILE"

if [ ! -e "$GPF_SUB_CONFIG_FILE_PATH" ]; then
    cp -v "$GPF_DATA_PATH/.gpf-config.sample" "$GPF_SUB_CONFIG_FILE_PATH"
fi

git config -ef "$GPF_SUB_CONFIG_FILE_PATH"
